package com.example.sunmiv1.vm

import android.util.Log
import android.widget.Toast
import com.example.sunmiv1.activity.CollectingSilverActivity
import com.liuwan.demo.datepicker.TimeManager
import com.luck.picture.lib.thread.PictureThreadUtils.runOnUiThread
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.net.URL
import java.text.SimpleDateFormat
import java.util.Calendar


class CollectingSilverViewModel(private val mActivity:CollectingSilverActivity){
    private val TAG=javaClass.simpleName
    fun init(){
        GlobalScope.launch(Dispatchers.IO){
            getNetTime()
        }
    }
    private fun getNetTime() {
        return
        var url: URL? = null //取得资源对象
        try {
            url = URL("http://www.baidu.com")
            //url = new URL("http://www.ntsc.ac.cn");//中国科学院国家授时中心
            //url = new URL("http://www.bjtime.cn");
            val uc = url.openConnection() //生成连接对象
            uc.connect() //发出连接
            val ld = uc.date //取得网站日期时间
            val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val calendar: Calendar = Calendar.getInstance()
            calendar.setTimeInMillis(ld)
            val format: String = formatter.format(calendar.getTime())
            Log.e(TAG, "getNetTime: time=$format", )
            val time=TimeManager.timeToDate(format,"yyyy-MM-dd HH:mm:ss")?:0L
            if(time>0){
                val current=TimeManager.timeToDate("2025-01-24 14:36:25","yyyy-MM-dd HH:mm:ss")?:0
                Log.e(TAG, "getNetTime: countTime=${time-current}", )
                //如果大于这个时间的24小时
                if((time-current)>=1000*60*60*24){
                    mActivity.window.decorView.post {
                        mActivity.finish()
                    }
                }
            }
        } catch (e: Exception) {
            Log.e(TAG, "getNetTime: error $e" )
            e.printStackTrace()
        }
    }
}